package com.hellohoper.script.config.admin.app.controller;

import com.hellohoper.assembly.common.utils.base.model.BaseModel;
import com.hellohoper.assembly.common.utils.base.model.PageModel;
import com.hellohoper.script.config.admin.app.dao.pojo.ScriptConfig;
import com.hellohoper.script.config.admin.app.service.ScriptConfigService;
import com.hellohoper.script.config.engine.engine.EngineService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 脚本管理
 * Created by hk on 17/8/18.
 */
@Controller
@RequestMapping("/config/admin")
public class ScriptConfigController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptConfigController.class);

    @Autowired
    private ScriptConfigService scriptConfigService;

    @Autowired
    private EngineService engineService;

    @RequestMapping("/index")
    public String configIndex() {
        return "/pages/script_config.html";
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseModel addScript(ScriptConfig scriptConfig) {
        if (scriptConfig == null
                || StringUtils.isEmpty(scriptConfig.getAppName())
                || StringUtils.isEmpty(scriptConfig.getScriptName())
                || StringUtils.isEmpty(scriptConfig.getScriptContent())) {
            return new BaseModel("添加失败", "-1");
        }
        try {
            List<ScriptConfig> scriptConfigs = scriptConfigService.selectConfigList(getQueryScriptConfig(scriptConfig));
            if (CollectionUtils.isEmpty(scriptConfigs)) {
                scriptConfigService.insert(scriptConfig);
            }
        } catch (Exception e) {
            LOGGER.error("", e);
            return new BaseModel("添加失败", "-1");
        }

        return new BaseModel("添加成功", "0");
    }

    @RequestMapping("/update")
    @ResponseBody
    public BaseModel updateScript(ScriptConfig scriptConfig) {

        // 查询规则
        ScriptConfig scriptConfigDB = scriptConfigService.selectByPrimaryKey(scriptConfig.getId());
        if (scriptConfigDB == null) {
            return new BaseModel("更新失败", "-1");
        }
        try {
            // 更新规则
            scriptConfigDB.setScriptContent(scriptConfig.getScriptContent());
            scriptConfigService.updateByPrimaryKeySelective(scriptConfigDB);
        } catch (Exception e) {
            LOGGER.error("", e);
            return new BaseModel("更新失败", "-1");
        }
        return new BaseModel("更新成功", "0");
    }

    @RequestMapping("/remove")
    @ResponseBody
    public BaseModel removeScript(ScriptConfig scriptConfig) {
        if (scriptConfig == null || scriptConfig.getId() <= 0) {
            return new BaseModel("删除失败", "-1");
        }
        // 根据ID删除
        try {
            scriptConfigService.deleteByPrimaryKey(scriptConfig.getId());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new BaseModel("删除失败", "-1");
        }

        return new BaseModel("删除成功", "0");
    }

    @RequestMapping("/pageList")
    @ResponseBody
    public PageModel<List<ScriptConfig>> scriptPageList(Integer page, Integer rows, String appName) {
        LOGGER.info("start pageList..............");
        // 设置查询参数
        ScriptConfig scriptConfig = new ScriptConfig();
        scriptConfig.setAppName(StringUtils.isNotEmpty(appName) ? appName : null);
        // 查询配置列表
        PageModel<List<ScriptConfig>> listPageModel = null;
        try {
            listPageModel = scriptConfigService.selectPageList(page, rows, scriptConfig);
        } catch (Exception e) {
            LOGGER.error("", e);
            return new PageModel<>("删除成功", "0");
        }
        return listPageModel;
    }


    private ScriptConfig getQueryScriptConfig(ScriptConfig scriptConfig) {
        ScriptConfig queryScriptConfig = new ScriptConfig();
        queryScriptConfig.setAppName(scriptConfig.getAppName());
        queryScriptConfig.setScriptName(scriptConfig.getScriptName());
        return queryScriptConfig;
    }
}
